package mysql

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ LabelModel = (*customLabelModel)(nil)

type (
	// LabelModel is an interface to be customized, add more methods here,
	// and implement the added methods in customLabelModel.
	LabelModel interface {
		labelModel
		List(ctx context.Context) ([]*Label, error)
		withSession(session sqlx.Session) LabelModel
	}

	customLabelModel struct {
		*defaultLabelModel
	}
)

func (m *customLabelModel) List(ctx context.Context) ([]*Label, error) {
	query := fmt.Sprintf("select %s from %s  ", labelRows, m.table)
	var resp []*Label
	err := m.conn.QueryRowsCtx(ctx, &resp, query)
	switch err {
	case nil:
		return resp, nil
	case sqlx.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

// NewLabelModel returns a model for the database table.
func NewLabelModel(conn sqlx.SqlConn) LabelModel {
	return &customLabelModel{
		defaultLabelModel: newLabelModel(conn),
	}
}

func (m *customLabelModel) withSession(session sqlx.Session) LabelModel {
	return NewLabelModel(sqlx.NewSqlConnFromSession(session))
}
